package yao.xiao.liu;

import java.lang.reflect.Field;

import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class AlwaysMarqueTextView extends TextView{
	private boolean isFirst=true;
	 private static final int MARQUEE_PIXELS_PER_SECOND = 30;
	  private static final int MARQUEE_RESOLUTION = 1000 / 30;
	Context mContext;
	public AlwaysMarqueTextView(Context context) {
		super(context);
		//this.mContext=context;
		
	}
	

	public AlwaysMarqueTextView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		this.mContext=context;
		
		// TODO Auto-generated constructor stub
	}


	public AlwaysMarqueTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext=context;
		
		// TODO Auto-generated constructor stub
	}


	@Override
	public boolean isFocused() {
		if(isFirst){
		try {
			Matrix matrix=new Matrix();
			
			@SuppressWarnings("rawtypes")
			Class clazz=  Class.forName("android.widget.TextView");
			Log.d("Class.forName---------->",""+ clazz);
			Field filed=clazz.getDeclaredField("mMarquee");
			filed.setAccessible(true);
			try {
				//Class clazz1=filed.getDeclaringClass();
				Object marquee=filed.get(this);		
				if(marquee!=null){
				 Class clazz1=marquee.getClass();
				//Class clazz1=Class.forName("android.widget.TextView.Marquee");
				//Class clazz1=marquee.getClass();
				Log.d("marquee.getClass()",""+ marquee);
				 Field field1= clazz1.getDeclaredField("mScrollUnit");
				 field1.setAccessible(true);
				 field1.setFloat(marquee,5.0f*field1.getFloat(marquee));
				// Float mScrollUnit = (Float) field1.get(marquee);
				
				final float density = getResources().getDisplayMetrics().density;
				
				 //mScrollUnit=0f;
	         //  mScrollUnit = 100.0f*(MARQUEE_PIXELS_PER_SECOND * density) / MARQUEE_RESOLUTION;
				isFirst=false;
				}
				//Log.d("mScrollUnit-------------->", ""+mScrollUnit);
     
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		return true ;
	}

}
